package cn.wolfcode.luowowo.website.web.controller;

import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.service.IDestinationService;
import cn.wolfcode.luowowo.article.service.ITravelService;
import cn.wolfcode.luowowo.article.vo.TravelListVo;
import cn.wolfcode.luowowo.cache.service.ITravelStatisVOService;
import cn.wolfcode.luowowo.cache.service.IUserInfoRedisService;
import cn.wolfcode.luowowo.cache.util.RedisKeys;
import cn.wolfcode.luowowo.cache.vo.TravelStatisVO;
import cn.wolfcode.luowowo.comment.domain.PersonalCenter;
import cn.wolfcode.luowowo.comment.service.IPersonalCenterService;
import cn.wolfcode.luowowo.common.result.Result;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.member.service.IUserInfoService;
import cn.wolfcode.luowowo.search.service.IUserInfoTemplateService;
import cn.wolfcode.luowowo.website.web.annotation.RequireLogin;
import cn.wolfcode.luowowo.website.web.annotation.UserParam;
import cn.wolfcode.luowowo.website.web.util.CookiesUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSONObject;
import org.elasticsearch.common.recycler.Recycler;
import org.hibernate.validator.constraints.pl.REGON;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/mine")
public class homePageController {
    @Reference
    private IUserInfoService userInfoService; //用户服务
    @Reference
    private ITravelService travelService; //游记服务
    @Reference
    private IDestinationService destinationService;//目的地服务
    @Reference
    private ITravelStatisVOService travelStatisVOService;//顶
    @Reference
    private IUserInfoRedisService userInfoRedisService;//缓存
    //我的窝
    @RequireLogin//贴上这个需要登录后才能进入
    @RequestMapping("/homepage")
    public String homepage(@UserParam UserInfo userInfo, Model model, Long id,HttpServletRequest request, HttpServletResponse response){
        if(id!=null){
            UserInfo userInfo1 = userInfoService.get(id);
            userInfo = userInfo1;
        }
        //1我的窝里面,用户信息的显示
        model.addAttribute("userInfo",userInfo);
        //2通过author_id=#{id}查出该用户的所有游记
        List<TravelListVo> travelList = travelService.getTravelByAuthorId(userInfo.getId());
        model.addAttribute("travelList",travelList);

        //3通过该用户的id查询,该用户游记总数
        int i = travelService.getCountByAuthorId(userInfo.getId());
        model.addAttribute("i",i);

        //4我的关注,通过该用户id去获取我的关注的用户信息
        List<UserInfo> focusedUserInfo = userInfoService.getFocusedUserInfo(userInfo.getId());
        //focusedUserInfo 全部我关注的人信息
        model.addAttribute("focusedUserInfo",focusedUserInfo);

        //5当查询我的关注时,我就是粉丝,通过条件为我id,全部我关注的人查询出来
        //select count(be_focused_id) FROM userinfo_attention WHERE attention_id = 4
        int focusedCount = userInfoService.getFocusedUserInfoCountById(userInfo.getId());
        model.addAttribute("focusedCount",focusedCount);

        //6当查询粉丝时,我就是被关注的人,通过我id为条件,把全部关注我的人查询出来
        //select count(attention_id) FROM userinfo_attention WHERE be_focused_id = 4
        int fansCount = userInfoService.getFansUserInfoCountById(userInfo.getId());
        model.addAttribute("fansCount",fansCount);

        //7通过用户id查询出蜂蜜数量
        int honeyCount = userInfoService.getHoneyCountById(userInfo.getId());
        model.addAttribute("honeyCount",honeyCount);
        //查询所有点评
        List<PersonalCenter> personal = personalCenterService.getcomment(userInfo.getId());
        model.addAttribute("personal",personal);
        //查询点评总条数
        long count = personalCenterService.getCount(userInfo.getId());
        model.addAttribute("count",count);


        return "mine/homepage";
    }

    @RequestMapping("/travelThumbup")
    @ResponseBody
    public Object travelThumbup(Long tid,@UserParam UserInfo userInfo){
        boolean ret = travelStatisVOService.travelThumbup(tid,userInfo.getId());
        TravelStatisVO vo = travelStatisVOService.getTravelStatisVo(tid);
        return new AjaxResult(ret,"").addData(vo);
    }

    
    //我的游记
    @RequireLogin//贴上这个需要登录后才能进入
    @RequestMapping("/mytravelnotes")
    public String mytravelnotes(@UserParam UserInfo userInfo, Model model,Long id){
        if(id!=null){
            UserInfo userInfo1 = userInfoService.get(id);
            userInfo = userInfo1;
        }
        //用户信息的显示
        model.addAttribute("userInfo",userInfo);
        //通过author_id=#{id}查出该用户的所有游记
        List<TravelListVo> travelList = travelService.getTravelByAuthorId(userInfo.getId());
        model.addAttribute("travelList",travelList);

        //通过该用户的id查询,该用户游记总数
        int i = travelService.getCountByAuthorId(userInfo.getId());
        model.addAttribute("i",i);

        int viewnumCount  = travelService.getViewnumCountByAuthorId(userInfo.getId());
        model.addAttribute("viewnumCount",viewnumCount);

        int replynumCount = travelService.getReplynumCountByAuthorId(userInfo.getId());
        model.addAttribute("replynumCount",replynumCount);
        return "mine/mytravelnotes";
    }
    @Reference
    private IPersonalCenterService personalCenterService;
    //我的点评
    @RequireLogin//贴上这个需要登录后才能进入
    @RequestMapping("/review")
    public String review(@UserParam UserInfo userInfo,Model model,String type){
        model.addAttribute("userInfo",userInfo);
        return "mine/review";
    }


    @RequestMapping(value = "/p")
    @ResponseBody
    public Result review(@RequestBody JSONObject json){

        return personalCenterService.getByUserId(json);
    }


}
